package top.twoteam.equipment.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.twoteam.equipment.entity.DataDictionary;
import top.twoteam.equipment.entity.DataDictionaryExample;
import top.twoteam.equipment.mapper.DataDictionaryMapper;
import top.twoteam.equipment.entity.DataDictionaryVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author binghan
 * @version 1.0
 * @description: TODO 数据字典
 * @date 2022/6/2 21:26
 */
@Service
public class DataDictionaryService {

    @Autowired
    DataDictionaryMapper dataDictionaryMapper;


    public List<DataDictionaryVO> queryDataDictionary() {

        DataDictionaryExample dictionaryExample = new DataDictionaryExample();

        DataDictionaryExample.Criteria criteria = dictionaryExample.createCriteria();

        // 条件查询查全部数据
        List<DataDictionary> dataDictionaries = dataDictionaryMapper.selectByExample(dictionaryExample);

        // 创建返回数据字典值对象
        List<DataDictionaryVO> dataDictionaryVOList = new ArrayList<DataDictionaryVO>();

        for (DataDictionary dataDictionary : dataDictionaries) {
            // 查询等级为 1 的类型id
            if("1".equals(dataDictionary.getLevel().toString())) {
                DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
                dataDictionaryVO.setTypeName(dataDictionary.getDictionaryidname());
                List<String> typeChildName = new ArrayList<>();
                for (DataDictionary dictionary : dataDictionaries) {
                    if(dataDictionary.getDictionaryid().equals(dictionary.getParentid())) {
                        // 添加当前类型名下的子类型
                        typeChildName.add(dictionary.getDictionaryidname());
                    }
                }
                dataDictionaryVO.setTypeChildName(typeChildName);
                dataDictionaryVOList.add(dataDictionaryVO);
            }
        }
        return dataDictionaryVOList;
    }
}
